﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Candy.Web.Data;
using GalaSoft.MvvmLight.Command;
using Candy.Infrastructure;
using Candy.Model;
using Candy.Web.Model;
using GalaSoft.MvvmLight.Threading;

namespace Candy.ViewModel
{
    public class ProjectEntityViewModel : EntityViewModelBase<ProjectDto>
    {
        public string Role
        {
            get
            {
                if (IsInDesignMode)
                    return "Member";
                if (_userManager.AuthenticatedUser.IsInRole(RoleDefinition.FormatGuestRole(Entity.Id)))
                    return "Guest";
                else if (_userManager.AuthenticatedUser.IsInRole(RoleDefinition.FormatMemberRole(Entity.Id)))
                    return "Member";
                else if (_userManager.AuthenticatedUser.IsInRole(RoleDefinition.FormatOwnerRole(Entity.Id)))
                    return "Owner";
                else
                    return "No Access";

            }
            set
            {
                RaisePropertyChanged("Role");
            }
        }


        public bool UserCanLoad
        {
            get
            {
                return _userManager.AuthenticatedUser.IsInRole(RoleDefinition.FormatGuestRole(Entity.Id)) ||
                    _userManager.AuthenticatedUser.IsInRole(RoleDefinition.FormatMemberRole(Entity.Id)) ||
                   _userManager.AuthenticatedUser.IsInRole(RoleDefinition.FormatOwnerRole(Entity.Id));
            }
            set
            {
                RaisePropertyChanged("UserCanLoad");
            }
        }

        private bool _userCanLeave;
        public bool UserCanLeave
        {
            get
            {
                return UserCanLoad && !_userManager.AuthenticatedUser.IsInRole(RoleDefinition.FormatOwnerRole(Entity.Id));
            }
            set
            {
                RaisePropertyChanged("UserCanLeave");
            }
        }


        public ProjectEntityViewModel()
            : base()
        {


        }

        INavigationService _navservice;
        IDataService _dataService;

        IUserManager _userManager;
        public ProjectEntityViewModel(IDataService dataService, INavigationService navservice, IUserManager userManager, ProjectDto entity)
            : base(entity)
        {
            _userManager = userManager;
            _dataService = dataService;
            _navservice = navservice;
            //cut and paste this line to your ctor
            _load = new RelayCommand(ExecLoad, CanLoad);
            //cut and paste this line to your ctor
            _join = new RelayCommand(ExecJoin, CanJoin);
            //cut and paste this line to your ctor
            _leave = new RelayCommand(ExecLeave, CanLeave);
        }


        private RelayCommand _load;
        public RelayCommand Load
        {
            get
            {
                return _load;
            }
        }

        /// <summary>
        /// Checks whether the Load command is executable
        /// </summary>
        private bool CanLoad()
        {
            return true;
        }

        /// <summary>
        /// Executes the Load command 
        /// </summary>
        private void ExecLoad()
        {
            base.ResetError();
            _navservice.ShowProjectLoader(this);
            _dataService.Initialize(Entity, op =>
                {
                    if (op.HasError)
                    {
                        base.HandleDomainError(op, "Failed to load project.");
                    }
                    else
                    {
                        _navservice.Navigate(NavigationTargets.Project);
                    }
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                        {
                            _navservice.CloseProjectLoader();
                        });
                });

        }


        private RelayCommand _join;
        public RelayCommand Join
        {
            get
            {
                return _join;
            }
        }

        /// <summary>
        /// Checks whether the Join command is executable
        /// </summary>
        private bool CanJoin()
        {
            return true;
        }

        /// <summary>
        /// Executes the Join command 
        /// </summary>
        private void ExecJoin()
        {
            SendMembershipRequestViewModel vm = new SendMembershipRequestViewModel(_dataService, _navservice, Entity);
            _navservice.ShowMembershipRequestWindow(vm);
        }


        private RelayCommand _leave;
        public RelayCommand Leave
        {
            get
            {
                return _leave;
            }
        }

        /// <summary>
        /// Checks whether the Leave command is executable
        /// </summary>
        private bool CanLeave()
        {
            return true;
        }

        /// <summary>
        /// Executes the Leave command 
        /// </summary>
        private void ExecLeave()
        {
            MessageBox.Show("Not supported, yet!");
        }
    }
}
